module("luci.controller.admin.behavioranalysis", package.seeall)

function index()
	entry({"admin", "behavioranalysis"},template("admin_behav_analy/behavioranalysis"), _("Behavior Analysis"), 3)
	entry({"admin", "behavioranalysis", "cli_mac"}, call("cli_mac"), nil, nil).leaf = true
	entry({"admin", "behavioranalysis", "cli_acc_url"}, call("cli_acc_url"), nil, nil).leaf = true
end

function geturl(macVal)
	local sqlite3 = require('lsqlite3')
	local datbaseURL='/mnt/sda1/url.db'
	local file =io.open(datbaseURL, "r")     --judge  the existence of the device database
	local rv = { }
	local sqlCmd='select * from url where mac="'..macVal..'"'

	if not file then
			return ""
		end
		file:close()

		local Devdb = sqlite3.open(datbaseURL, sqlite3.OPEN_READWRITE)
		if Devdb==nil then
			return ""
		end

       -- local sqlCmd='SELECT * FROM url WHERE mac text='..macVal


		for row in Devdb:nrows(sqlCmd) do
			rv[#rv+1] = {
				url = row.url_value
			}
		end
		Devdb:close()

		return rv
end

function getMac()
    local s = require "luci.tools.status"
	return s.dhcp_leases()
end

function cli_acc_url(macVal)
    luci.http.prepare_content("application/json")
	luci.http.write('[')
	luci.http.write_json(geturl(macVal))
	luci.http.write(']')
end

function cli_mac()
    luci.http.prepare_content("application/json")
	luci.http.write('[')
	luci.http.write_json(getMac())
	luci.http.write(']')
end
